﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameLib.Main;
using GameLib.Entities;

namespace GameLib.WorldMap
{
	public class Tile
	{
		public TileEntityCollection Entities
		{
			get;
			private set;
		}

		public Terrain Terrain
		{
			get { return m_Terrain; }
			set
			{
				if (m_Terrain != value)
				{
					m_Terrain = value;

					if (TerrainChanged != null)
						TerrainChanged(this, null);
				}
			}
		}

		public event EventHandler TerrainChanged;
		public event EventHandler SettlementChanged;

		public Tile(Map map, int x, int y)
		{
			m_Map = map;
			X = x;
			Y = y;
			Terrain = Core.Play.World.Terrains.DefaultTerrain;
			Edges = new TileEdgeDesc[MapEdgeDirection.MaxDirs];
		}

		public int X
		{
			get;
			private set;
		}

		public int Y
		{
			get;
			private set;
		}

		public int AlignedX
		{
			get { return X; }
		}

		public int AlignedY
		{
			get { return Y / 2; }
		}

		public River GetRiver(MapEdgeDirection dir)
		{
			return Edges[dir.Index].River;
		}

		public void OnRiverAdded(River river, MapEdgeDirection dir)
		{
			Edges[dir.Index].River = river;
		}

		public void OnSettlementAdded(Settlement settlement)
		{
			Settlement = settlement;
		}

		public void OnSettlementRemoved(Settlement settlement)
		{
			if (Settlement == settlement)
				Settlement = null;
		}

		public Settlement Settlement
		{
			get { return _Settlement; }
			private set
			{
				if (_Settlement != value)
				{
					_Settlement = value;

					if (SettlementChanged != null)
						SettlementChanged(this, null);
				}
			}
		}
		private Settlement _Settlement = null;

		private TileEdgeDesc[] Edges { get; set; }

		private Map m_Map;
		private Terrain m_Terrain;		
	}

	public struct TileEdgeDesc
	{
		public River River;
	}

	public class TileEntityCollection : List<Entity>
	{
	
	}
}
